package com.cos.hospital.widget.baseadapter;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.text.TextUtils;


import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


/**
 * author:  yangliu
 * date:    2018-03-08
 * comment: 集合判断工具
 */
public class CollectionUtil {

    /**
     * 判断是否为空
     *
     * @param collection 集合
     * @return 空位true, 否则为false
     */
    public static <E> boolean isEmpty(Collection<E> collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * 判断是否为空
     *
     * @param map map
     * @return 空位true, 否则为false
     */
    public static <K, V> boolean isEmpty(Map<K, V> map) {
        return map == null || map.isEmpty();
    }

    /**
     * 保留二位小数
     *
     * @return 空位true, 否则为false
     */
    public static String longFormat(Double number) {
        DecimalFormat df = new DecimalFormat(",##0.00");
        return df.format(number);
    }


    /**
     * 判断是否包含字符串
     *
     * @param arrs 数组
     * @param val  是否包含的字符串
     * @return 包含返回true, 否则为false
     */
    public static boolean isContains(String[] arrs, String val) {
        if (arrs == null || TextUtils.isEmpty(val)) {
            return false;
        }
        for (String arrVal : arrs) {
            if (val.equals(arrVal)) {
                return true;
            }
        }
        return false;
    }

    public static Date formatLineToDate(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public static String longToLineTime(Long time) {
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static Date formatDianToDate(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        Date parse = null;
        try {
            parse = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public static String getDateToString(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static String getDataToStringSpot(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        return format.format(date);
    }

    public static String getNowTime(Long mLong) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        return format.format(new Date(mLong));
    }

    /**
     * 获取版本信息
     *
     * @return
     * @throws Exception
     */
    public static String getVersionName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            String version = packInfo.versionName;
            return version;
        } catch (PackageManager.NameNotFoundException e) {
        }
        return "";
    }



    //版本号比较
    public static int appVersionToInt(String version) {
        return bytesToInt(versionToBytesByReg(version));
    }

    private static byte[] versionToBytesByReg(String versionString) {
        byte[] ret = new byte[4];
        try {
            String[] versionArr = versionString.split("\\.");
            ret[0] = (byte) (Integer.parseInt(versionArr[0]) & 0xFF);
            ret[1] = (byte) (Integer.parseInt(versionArr[1]) & 0xFF);
            ret[2] = (byte) (Integer.parseInt(versionArr[2]) & 0xFF);

        } catch (Exception e) {

        }
        return ret;
    }

    private static int bytesToInt(byte[] bytes) {
        int addr = bytes[2] & 0xFF;
        addr |= ((bytes[1] << 8) & 0xFF00);
        addr |= ((bytes[0] << 16) & 0xFF0000);
        return addr;
    }

    public static long meidaPlayerLong(String path) throws IOException {

        MediaPlayer meidaPlayer = new MediaPlayer();

        meidaPlayer.setDataSource(path);

        meidaPlayer.prepare();

        return meidaPlayer.getDuration();//获得了视频的时长（以毫秒为单位）
    }
}
